/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.controller;

import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.manager.entity.PARTNER;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@ViewScoped
public class PartnerController implements Serializable{
    private List<PARTNER> listPartner = new ArrayList<PARTNER>();
    private LazyDataModel<PARTNER> lazyPartnerModel;
    private PARTNER current = new PARTNER();
    private String nameS = "";
    /**
     * Creates a new instance of PartnerController
     */
    public PartnerController() {
        try {
            listPartner = PARTNER.getAll("");
            lazyPartnerModel = new LazyAbstractDataModel<PARTNER>(listPartner);
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
    }

    public LazyDataModel<PARTNER> getLazyPartnerModel() {
        return lazyPartnerModel;
    }

    public void setLazyPartnerModel(LazyDataModel<PARTNER> lazyPartnerModel) {
        this.lazyPartnerModel = lazyPartnerModel;
    }

    public String getNameS() {
        return nameS;
    }

    public void setNameS(String nameS) {
        this.nameS = nameS;
    }

    public List<PARTNER> getListPartner() {
        return listPartner;
    }

    public void setListPartner(List<PARTNER> listPartner) {
        this.listPartner = listPartner;
    }

    public PARTNER getCurrent() {
        return current;
    }

    public void setCurrent(PARTNER current) {
        this.current = current;
    }
    
    public String insert(){
        try {
            if(current.insert()){
                listPartner.add(current);
                lazyPartnerModel = new LazyAbstractDataModel<PARTNER>(listPartner);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thành công!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thất bại!"));            
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        } finally{
            current = new PARTNER();
        }
        return "";
    }
    
    public String search(){
        try {
            listPartner = PARTNER.getAll(" and upper(partner_name) like '%"+nameS.toUpperCase()+"%'");
            int i = listPartner.size();
            lazyPartnerModel = new LazyAbstractDataModel<PARTNER>(listPartner);
            if(i>0){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có "+i+" bản ghi được tìm thấy!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
    
    public String prepareDlgEdit(PARTNER p){
        try{
            current = (PARTNER) p.clone();
        }catch(Exception e){
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
    
    public String update(){
        try {
            if(current.update()){
                for(int i=0;i<listPartner.size();i++){
                    if(listPartner.get(i).getPartner_id() == current.getPartner_id()){
                        listPartner.set(i, current);
                        break;
                    }
                }
                lazyPartnerModel = new LazyAbstractDataModel<PARTNER>(listPartner);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Sửa bản ghi thành công!"));
            }
            else{
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Sửa bản ghi thất bại!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        } finally{
            current = new PARTNER();
        }
        return "";
    }
    
    public String remove(PARTNER p){
        try {
            if(p.remove()){            
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));        
                listPartner.remove(p);
                lazyPartnerModel = new LazyAbstractDataModel<PARTNER>(listPartner);
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thất bại!"));        
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
}
